﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KnowlegeAssistant
{
    public partial class EditUserForm : Form
    {
        private ActionType action;
        private User user = null;
        
        public enum ActionType { edit, add}
        
        public EditUserForm()
        {
            InitializeComponent();
            this.action = ActionType.add;
            RefreshData();
            this.Text = "AddUser";
        }

        public EditUserForm(int user_id)
        {
            InitializeComponent();
            this.action = ActionType.edit;
            this.user = UserUtil.GetUser(user_id);
            RefreshDataForEdit();
            addbutton.Text = "Изменить";
            this.Text = "EditUser";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RefreshDataForEdit()
        {
            logintextbox.Enabled = false;
            this.passwordtextbox.Text = user.Password;
            this.passwordchecktextbox.Text = user.Password;
            this.logintextbox.Text = user.Name;

            this.rolecombobox.DataSource = RoleUtil.getListRoles();
            this.rolecombobox.DisplayMember = "Name";

            foreach (Role r in rolecombobox.Items)
            {
                if (r.Name == user.Role.Name)
                {
                    rolecombobox.SelectedItem = r;
                }
            }

            if (user.OwnerType == 0)
            {
                adminradioButton.Checked = true;
            }
            else if (user.OwnerType == 1)
            {
                studentradioButton.Checked = true;
                this.ownercomboBox.Text = ((Student)user.Owner).FullName;
            }
            else if (user.OwnerType == 2)
            {
                teacherradioButton.Checked = true;
                this.ownercomboBox.Text = ((Teacher)user.Owner).FullName;
            }

            this.adminradioButton.Enabled = false;
            this.studentradioButton.Enabled = false;
            this.teacherradioButton.Enabled = false;

            this.ownercomboBox.Enabled = false;
        }

        private void RefreshData()
        {
            this.rolecombobox.DataSource = RoleUtil.getListRoles();
            this.rolecombobox.DisplayMember = "Name";

            this.ownercomboBox.DataSource = StudentUtil.GetListStudentsForAddUser();
            this.ownercomboBox.DisplayMember = "FullName";
        }

        private void addbutton_Click(object sender, EventArgs e)
        {
            if (action == ActionType.add)
            {
                AddUser();
            }
            else if (action == ActionType.edit)
            {
                EditUser();
            }

            this.Close();
        }

        private void AddUser()
        {
            string name = logintextbox.Text.Trim();
            if (name != "" && !UserUtil.Exists(name) && !name.Contains(" "))
            {
                string password = passwordtextbox.Text.Trim();
                string passwordcheck = passwordchecktextbox.Text.Trim();

                int role_id = ((Role)rolecombobox.SelectedItem).RoleID;

                int owner_id = 0;
                int owner_type_id = 0;

                if (studentradioButton.Checked)
                {
                    owner_id = ((Student)ownercomboBox.SelectedItem).StudentID;
                    owner_type_id = 1;
                }
                else if (teacherradioButton.Checked)
                {
                    owner_id = ((Teacher)ownercomboBox.SelectedItem).TecherID;
                    owner_type_id = 2;
                }
                UserUtil.AddUser(name, password, role_id, owner_id, owner_type_id);
            }
        }

        private void EditUser()
        {
            DatabaseConnect.execSP("EditUser", 
                new List<string>() { 
                    user.ID.ToString(), 
                    passwordtextbox.Text.Trim(), 
                    ((Role)rolecombobox.SelectedItem).RoleID.ToString() 
                });
        }

        private void teacherradioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (teacherradioButton.Checked == true)
            {
                this.ownercomboBox.Enabled = true;
                this.ownercomboBox.DataSource = TeacherUtil.GetListTeachers();
                this.ownercomboBox.DisplayMember = "FullName";
                EnableRoles();
            }
        }

        private void adminradioButton_CheckedChanged(object sender, EventArgs e)
        {
            this.ownercomboBox.DataSource = null;
            this.ownercomboBox.Enabled = false;
            this.rolecombobox.DataSource = new List<Role>() { RoleUtil.getRole("Admin") };
            this.rolecombobox.DisplayMember = "Name";
            this.rolecombobox.Enabled = false;
        }

        private void studentradioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (studentradioButton.Checked == true)
            {
                this.ownercomboBox.Enabled = true;
                this.ownercomboBox.DataSource = StudentUtil.GetListStudentsForAddUser();
                this.ownercomboBox.DisplayMember = "FullName";
                EnableRoles();
            }
        }

        private void EnableRoles()
        {
            this.rolecombobox.Enabled = true;
            this.rolecombobox.DataSource = RoleUtil.getListRoles();
            this.rolecombobox.DisplayMember = "Name";
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                this.passwordtextbox.PasswordChar = '\0';
                this.passwordchecktextbox.PasswordChar = '\0';
            }
            else
            {
                this.passwordtextbox.PasswordChar = '*';
                this.passwordchecktextbox.PasswordChar = '*';
            }
        }
    }
}
